package com.woorigil.social.parser.coupang;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Component;

import com.woorigil.social.Context;
import com.woorigil.social.domain.deal.Deal;
import com.woorigil.social.parser.CategoryMapper;

@Component (value="CPMapper")
public class CoupangCategoryMapper extends CategoryMapper {
	static Map<Integer, List<String>> shoppings = new HashMap<Integer, List<String>>() {{
		put(2, Arrays.asList("의류"));
		put(3, Arrays.asList("패션잡화"));
		put(4, Arrays.asList("뷰티"));
		put(5, Arrays.asList("식품"));
		put(6, Arrays.asList("스포츠/레저"));
		put(7, Arrays.asList("디지털"));
		put(8, Arrays.asList("가전"));	//가전
		put(9, Arrays.asList("출산/유아동"));
		put(10, Arrays.asList("생활/주방"));
		put(11, Arrays.asList("홈/데코/취미"));
		put(12, Arrays.asList("쇼핑몰 이용권", "쇼핑몰 할인권"));
	}};		
	
	@Override
	public int getCategoryId(Deal deal) {
		int subCategoryId = 0;
		if ("여행/레저".equals(deal.getCategory())) {
			subCategoryId = Context.getSubCategoryId(DEPTH_ONE_CATEGORY_TRAVEL, getSubCategoryName(deal.getSection1()));
		} else if ("문화".equals(deal.getCategory())) {
			subCategoryId = Context.getSubCategoryId(DEPTH_ONE_CATEGORY_CULTURE, getSubCategoryName(deal.getSection2()));
		} else if ("지역".equals(deal.getCategory())) {
			int mainCategoryId = getCategoryIdFromLocal(deal);
			subCategoryId = Context.getSubCategoryId(mainCategoryId, getSubLocalName(deal));
		} else if ("쇼핑".equals(deal.getCategory())) {
			String categoryName = deal.getCategoryName();
			if ("쇼핑몰 이용권".equals(categoryName)) {
				categoryName = "온라인 이용권";
			}
			subCategoryId = extractShoppingCategoryId(deal);
		}
		return (subCategoryId == 0)? 100 : subCategoryId;
	}
	
	public int extractShoppingCategoryId(Deal deal) {
		for (Entry<Integer, List<String>> entry : shoppings.entrySet()) {
			for (String each : entry.getValue()) {
				if (each.equals(deal.getSection1())) {
					return entry.getKey();
				}
			}
		}
		return 100;
	}		
	
	private String getSubLocalName(Deal deal) {
		for (String local : locals) {
			if (deal.getShopAddress() != null && deal.getShopAddress().contains(local)) {
				if ("인천".equals(local) || "부천".equals(local)) {
					local = "인천/부천";
				}
				return local;
			}
		}
		return "전국";
	}	

	protected String getSubCategoryName(String category) {
		if ("전남".contains(category) || "전북".contains(category)) {
			return "전라";
		} else if ("경남".contains(category) || "경북".contains(category)) {
			return "경상";
		} else if ("충남".contains(category) || "충북".contains(category)) {
			return "충청";
		}
		return category;
	}
}
